<?php 
class UsersController extends AppController {
	public $Helpers = array ('Html' , 'Form' ); 
	public $uses = array('Weibo', 'Comment', 'Thread', 'Message', 'User', 'Follow');

	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('signin');
	}

	public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$myuser = $this->User->findByUsername($this->request->data['User']['username']);
				$this->Session->write('username', $myuser['User']['username']);
				$this->Session->write('userid', $myuser['User']['id']);
				return $this->redirect($this->Auth->redirect());
			}
			$this->Session->setFlash(__('Invalid username or password,try again'));
		}
	}

	public function logout() {
		$this->Session->write('username', null);
		$this->Session->write('userid', null);
		return $this->redirect($this->Auth->logout());
	}

	public function index() {
		if ( $userId = $this->Session->read('userid') )
			return $this->redirect(array('action' => 'view', $userId));
		return $this->redirect(array('controller' => 'weibos', 'action' => 'index'));
	}

	public function view($id = null) {
		$post = $this->User->findById($id);
		$this->set('post', $post);

		$weibosCount = $this->Weibo->find('count' , array( 'conditions' => array('user_id' => $id)));
		$this->set('weibosCount', $weibosCount);

		$followsCount = $this->Follow->find('count' , array( 'conditions' => array('from' => $id)));
		$this->set('followsCount', $followsCount);

		$fansCount = $this->Follow->find('count' , array( 'conditions' => array('to' => $id)));
		$this->set('fansCount', $fansCount);

	}

	public function signin() {
		if ($this->request->is('post')) {
			$this->User->create();
			$this->request->data['User']['date'] = date('Y-m-d H:i:s');
			$this->request->data['User']['role'] = 'author';
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				return $this->redirect(array( 'controller' => 'weibos', 'action' => 'index'));
			}
			$this->Session->setFlash(__('The user could not be saved.Please,try again.'));
		}
	}

	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFonundException(__('Invalid user'));
		}

		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->savee($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				return $this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
		} else {
			$this->request->data = $this->User->read(null,$id);
			unset($this->request->data['User']['password']);
		}
	}

	public function delete($id = null) {
		$this->request->onlyAllow('post');

		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFonundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			return $this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		return $this->redirect(array('action' => 'index'));
	}

	public function follow($toId = null) {
		if (!$toId) {
			throw new NotFoundException(__('Invalid post'));
		}

		$fromId = $this->Session->read('userid');
		$id = $this->Follow->findByFromAndTo($fromId, $toId);
		if ($id) {
			$this->Session->setFlash(__('You has already followed'));
		} else {
			$this->Follow->create();
			$data = array('from' => $fromId, 'to' => $toId);
			if ( $this->Follow->save($data)) {
				$this->Session->setFlash(__('Followed succeed'));
			} else {
				$this->Session->setFlash(__(
					'Followed failed! Please try again'));
			}
		}
		return $this->redirect(array('action' => 'view', $toId));
	}

	public function unFollow($toId = null) {
		if (!$toId) {
			throw new NotFoundException(__('Invalid post'));
		}
		$fromId = $this->Session->read('userid');
		$follow = $this->Follow->findByFromAndTo($fromId, $toId);

		if ($follow) {
			$this->Follow->delete($followi['Follow']['id']);
			$this->Session->setFlash(__('Unfollow succeed'));
		} else {
			$this->Session->setFlash(__('Unfollow failed'));
		}
		return $this->redirect(array('action' => 'view', $toId));
	}

	public function fansView($userId = null) {
		$toId = $userId;
		$this->set('fans', $this->Follow->findByTo($toId));
	}

	public function followedView($userId = null){
		$fromId = $userId;
		$this->set('follower', $this->Follow->findByFrom($fromId));
	}

}
?>
